package com.enterprise.service.impl;

import com.enterprise.dao.RoleresDao;
import com.enterprise.dao.ServersManage;
import com.enterprise.entity.Roleres;
import com.enterprise.service.RoleresService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("roleresService")
public class RoleresServiceImpl extends ServersManage<Roleres, RoleresDao> implements RoleresService {

	@Resource(name="roleresDao")
	@Override
	public void setDao(RoleresDao roleresDao) {
		this.dao = roleresDao;
	}

	@Override
	public int setResourcesRole(String[] roleIds, String resourcesId) {
		for (int i =0;i<roleIds.length;i++){
			Roleres roleres = new Roleres();
			roleres.setResourcesid(Long.valueOf(resourcesId));
			roleres.setRoleid(Long.valueOf(roleIds[i]));
			Roleres roleres1 = this.dao.selectByRoleres(roleres);
			if (roleres1 == null){
				this.dao.insert(roleres);
			}
		}
		return 1;
	}

	@Override
	public int delResourcesRole(String[] roleIds, String resourcesId) {
		for (int i =0;i<roleIds.length;i++){
			Roleres roleres = new Roleres();
			roleres.setResourcesid(Long.valueOf(resourcesId));
			roleres.setRoleid(Long.valueOf(roleIds[i]));
			this.dao.deleterel1(roleres);
		}
		return 1;
	}


	@Override
	public int setRoleResources(String[] resourcesIds, String roleId) {
		for (int i =0;i<resourcesIds.length;i++){
			Roleres roleres = new Roleres();
			roleres.setResourcesid(Long.valueOf(resourcesIds[i]));
			roleres.setRoleid(Long.valueOf(roleId));
			Roleres roleres1 = this.dao.selectByRoleres(roleres);
			if (roleres1 == null){
				this.dao.insert(roleres);
			}
		}
		return 1;
	}

	@Override
	public int delRoleResources(String[] resourcesIds, String roleId) {
		for (int i =0;i<resourcesIds.length;i++){
			Roleres roleres = new Roleres();
			roleres.setResourcesid(Long.valueOf(resourcesIds[i]));
			roleres.setRoleid(Long.valueOf(roleId));
			this.dao.deleterel1(roleres);
		}
		return 1;
	}

}
